package com.stu.core.mysite_core;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	private static String driver;
	private static String dburl;
	private static String user;
	private static String passwd;
	
	private static final ConnectionFactory factory = new ConnectionFactory();
	
	private Connection conn;
	
	//从配置文件中读取数据到变量中
	static{
		Properties prop = new Properties();
		try {
			InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
			prop.load(in);
		} catch (Exception e) {
			System.out.println("配置文件dbconfig.properties读取错误！"+e.getMessage());
		}
		driver = prop.getProperty("driver");
		dburl = prop.getProperty("dburl");
		user = prop.getProperty("user");
		passwd = prop.getProperty("passwd");
		
		
	}
	
	private ConnectionFactory() {
		
	}
	
	public static ConnectionFactory getInstance(){
		return factory;
	}
	
	public Connection makeConnection(){
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(dburl, user, passwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) throws SQLException {
		Connection conn = ConnectionFactory.getInstance().makeConnection();
		System.out.println(conn.getAutoCommit());
	}
}
